﻿/*
 * Created by SharpDevelop.
 * User: StrunevskyDV
 * Date: 01.03.2010
 * Time: 16:25
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Timers;


namespace EmailJabber.Logic.Logic.BusinessLogic
{
	/// <summary>
	/// Description of CommandProcessor.
	/// </summary>
	public class CommandProcessor
	{
        private static CommandProcessor instance = new CommandProcessor();

        public static Guid FROM_NAME = new Guid("0800BFCD-4BEE-4954-BCA6-59BC5B9E808C");
        
        private List<PendingCommandEntry > pendingCommands = new  List<PendingCommandEntry >();
		private List<PendingCommandEntry> pendingTimerCommands = new  List<PendingCommandEntry>();
		
        
        public static CommandProcessor Instance
        {
            get
            {
                return instance;
            }
        }

        private CommandProcessor()
        {
        	Timer tmr = new Timer();
            tmr.Elapsed += OnTimedEvent;
            tmr.Interval = 60000;
            tmr.Enabled = true;
        }
        
        private void OnTimedEvent(object sender, ElapsedEventArgs e)
        {
        	List<PendingCommandEntry> result = new List<PendingCommandEntry> ();
			foreach(PendingCommandEntry userCommand in pendingTimerCommands)
			{
				if(userCommand.ExecuteTime < DateTime.Now)
				{
					result.Add(userCommand);
				}
			}
			pendingTimerCommands.RemoveAll( item => item.ExecuteTime < DateTime.Now );

			foreach(PendingCommandEntry currentCommand in result)
			{
				MessageIM message = new MessageIM(FROM_NAME.ToString(),string.Empty,string.Empty);
				message.ExtraParameter = currentCommand.CommandParameter;
				BL.Instance.ExecuteCommandById(currentCommand.CommandId,message);
			}
        }
        
		public void PushCommand(string fromUser,Guid commandId)
		{
			PushCommand(fromUser,commandId,null);
		}
		
		public void PushCommand(string fromUser,Guid commandId,object param)
		{
			PendingCommandEntry val = new PendingCommandEntry(fromUser,commandId);
			val.CommandParameter = param;
			pendingCommands.Add(val);
		}
		
		public void PushCommandOnTime(Guid commandId,DateTime time)
		{
			PushCommandOnTime(commandId,time,null);
		}
		
		public void PushCommandOnTime(Guid commandId,DateTime time,object param)
		{
			PendingCommandEntry val = new PendingCommandEntry(commandId,time);
			val.CommandParameter=param;
			pendingTimerCommands.Add(val);
		}		
		
		public List<PendingCommandEntry> PopCommandsForUser(string userId)
		{
			List<PendingCommandEntry> result = new List<PendingCommandEntry> ();
			foreach(PendingCommandEntry userCommand in pendingCommands)
			{
				if(userCommand.FromUser.Equals(userId))
				{
					result.Add(userCommand);
				}
			}
			pendingCommands.RemoveAll( item => item.FromUser.Equals(userId));
			return result;
		}
	}
}
